package com.qst.order.config;

import com.qst.order.listener.OrderExpireListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class RedisListenerConfig {

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                                   OrderExpireListener orderExpireListener) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);

        // 监听所有数据库的过期事件（"__keyevent@*__:expired"）
        // 仅监听订单相关的键过期（可通过键前缀过滤）
        container.addMessageListener(orderExpireListener,
                new PatternTopic("__keyevent@*__:expired"));
        return container;
    }
}